<?php
/**
 * SmsService.php
 * 文件描述
 * Created on 2020/9/4 10:42
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\xiangji\services;
use app\common\caches\SmsCache;
use app\common\services\SmsService as CommonSmsService;
use app\common\helpers\Response;
use app\xiangji\exception\BadRequestException;

class SmsService
{
    private $smsService;

    public function __construct(CommonSmsService $smsService)
    {
        $this->smsService = $smsService;
    }

    /**
     * @todo 发送修改手机号短信验证码
     * @param string $phone 手机号
     * @return \AlibabaCloud\Client\Result\Result|bool
     * @throws \think\Exception
     */
    public function sendModifyMobileCode(string $phone)
    {
        // 判断是否已发送
        $has_send = SmsCache::getVerifyCode($phone);
        if ($has_send){
            throw new BadRequestException('操作太频繁，请稍后再试', Response::HTTP_TOO_MANY_REQUESTS);
        }
        $verify_code = createCode(6);
        $res = $this->smsService->sendVerifyCode($phone, $verify_code);
        SmsCache::setVerifyCode($phone, $verify_code);
        return $res;
    }
}
